package com.ruoyi.web.controller.light;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.toolkit.ChainWrappers;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.domain.light.LitConfigErr;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.mybatisplus.QueryWrTool;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.light.service.ILitConfigErrService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 安灯配置 - 异常类型Controller
 *
 * @author ruoyi
 * @date 2022-02-14
 */
@RestController
@RequestMapping("/light/config/err")
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class LitConfigErrController extends BaseController {

    private final ILitConfigErrService litConfigErrService;

    /**
     * 查询安灯配置 - 异常类型列表
     */
    @RequestMapping("/list")
    public TableDataInfo list(LitConfigErr litConfigErr) {
        return getDataTable(litConfigErrService.getList(litConfigErr));
    }

    /**
     * 查询安灯配置 - 异常类型
     */
    @RequestMapping("/getOne")
    public AjaxResult getOne(Long id) {
        return AjaxResult.success(litConfigErrService.getOne(id));
    }


    /**
     * 导出安灯配置 - 异常类型列表
     */
    @PreAuthorize("@ss.hasPermi('light:config:err')")
    @Log(title = "安灯配置 - 异常类型", businessType = BusinessType.EXPORT)
    @RequestMapping("/export")
    public void export(HttpServletResponse response, LitConfigErr litConfigErr) {
        List<LitConfigErr> list = litConfigErrService.list(QueryWrTool.queryWrapperByBaseEntity(litConfigErr));
        ExcelUtil<LitConfigErr> util = new ExcelUtil<>(LitConfigErr.class);
        util.exportExcel(response, list, "安灯配置 - 异常类型数据");
    }

    /**
     * 新增安灯配置 - 异常类型
     */
    @PreAuthorize("@ss.hasPermi('light:config:err')")
    @Log(title = "安灯配置 - 异常类型", businessType = BusinessType.INSERT)
    @RequestMapping("/addSave")
    public AjaxResult addSave(@RequestBody LitConfigErr litConfigErr) {
        if (StringUtils.isBlank(litConfigErr.getErrName())) {
            return AjaxResult.error("名称不可为空");
        }
        long count = litConfigErrService.count(Wrappers.lambdaQuery(LitConfigErr.class).
                eq(LitConfigErr::getErrName, litConfigErr.getErrName()));
        if (count > 0) {
            return AjaxResult.error("名称已存在");
        }
        return toAjax(litConfigErrService.addSave(litConfigErr));
    }


    /**
     * 修改安灯配置 - 异常类型
     */
    @PreAuthorize("@ss.hasPermi('light:config:err')")
    @Log(title = "安灯配置 - 异常类型", businessType = BusinessType.UPDATE)
    @RequestMapping("/editSave")
    public AjaxResult edit(@RequestBody LitConfigErr litConfigErr) {
        if (StringUtils.isBlank(litConfigErr.getErrName())) {
            return AjaxResult.error("名称不可为空");
        }
        if (litConfigErr.getErrId().equals(litConfigErr.getParentId())) {
            return AjaxResult.error("上级不能选择自己");
        }
        long count = litConfigErrService.count(Wrappers.lambdaQuery(LitConfigErr.class).
                ne(LitConfigErr::getErrId, litConfigErr.getErrId()).
                eq(LitConfigErr::getErrName, litConfigErr.getErrName()));

        if (count > 0) {
            return AjaxResult.error("名称已存在");
        }

        return toAjax(litConfigErrService.editSaveById(litConfigErr));
    }


    /**
     * 删除安灯配置 - 异常类型
     */
    @PreAuthorize("@ss.hasPermi('light:config:err')")
    @Log(title = "安灯配置 - 异常类型", businessType = BusinessType.DELETE)
    @RequestMapping("/remove")
    public AjaxResult remove(String ids) {
        List<Long> idList = Convert.toLongList(ids);
        boolean exists = ChainWrappers.lambdaQueryChain(litConfigErrService.getBaseMapper()).in(LitConfigErr::getParentId, idList).exists();
        if (exists) {
            return AjaxResult.error("存在子级,不允许删除");
        }

        return toAjax(litConfigErrService.del(idList));
    }

}